package com.sinosoft.xmlTransformer;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.StringReader;
import java.io.StringWriter;
import java.util.Properties;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Result;
import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.TransformerFactoryConfigurationError;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;

public class XmlToString {

	public void stringToXml(String xmlStr) {
		System.out.println(xmlStr);
		StringReader sr = new StringReader(xmlStr);
		InputSource is = new InputSource(sr);
		DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
		DocumentBuilder builder;
		Transformer transformer;
		Properties prop = new Properties();
		try {
			// 加载xml的配置
			prop.load(XmlToString.class.getClassLoader().getResourceAsStream(
					"xml.properties"));

		} catch (IOException e) {
			e.printStackTrace();
		}
		String lmx = prop.getProperty("lmx");
		File xml = new File(lmx);
		if (!xml.exists()) {
			try {
				xml.createNewFile();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}

		try {

			builder = factory.newDocumentBuilder();

			Document doc = builder.parse(is);

			Source source = new DOMSource(doc);

			// Source source =new DOMSource(doc);

			Result result = new StreamResult(xml);

			transformer = TransformerFactory.newInstance().newTransformer();
			transformer.setOutputProperty(OutputKeys.INDENT, "yes");

			transformer.transform(source, result);
			

		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	public String createXml() {
		String xmlString = "";

		DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
		try {
			DocumentBuilder builder = factory.newDocumentBuilder();
			Properties prop = new Properties();
			try {
				// 加载xml的配置
				prop.load(XmlToString.class.getClassLoader()
						.getResourceAsStream("xml.properties"));

			} catch (IOException e) {
				e.printStackTrace();
			}
			//将xml转化为字符串
			String xml = prop.getProperty("lmx");
			File file = new File(xml);
			Document doc = builder.parse(file);
			Source source = new DOMSource(doc);
			StringWriter stringWriter = new StringWriter();
			Result result = new StreamResult(stringWriter);
			TransformerFactory factory2 = TransformerFactory.newInstance();
			Transformer transformer = factory2.newTransformer();
			transformer.setOutputProperty(OutputKeys.INDENT, "yes");
			transformer.transform(source, result);
			xmlString = stringWriter.getBuffer().toString();
			// System.out.println(xmlString);

		} catch (Exception e) {
			e.printStackTrace();
		}

		return xmlString;
	}
}
